package servlet;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.persistence.Query;

import entities.Game;
import entities.User;

public class DatabaseAccess
{
	EntityManager em;

	public DatabaseAccess()
	{
		em = Persistence.createEntityManagerFactory("epistocracy")
				.createEntityManager();
	}

	public List<User> getAlleUser()
	{
		Query q = em.createNamedQuery("AlleUser");
		List<User> liste = q.getResultList();

		return liste;
	}
	
	public List<User> getRanking()
	{
		Query q = em.createQuery("SELECT u FROM User u ORDER BY u.ranking");
		List<User> liste = q.getResultList();

		return liste;
	}

	public void speichern(User u)
	{
		em.getTransaction().begin();
		em.persist(u);
		em.getTransaction().commit();
	}

	public void speichern(Game g)
	{
		em.getTransaction().begin();
		em.persist(g);
		em.getTransaction().commit();
	}
}
